import java.util.*;

/*
 *  @author  Alpesh Mistry  <apm1dec@gmail.com>
 *  @author  Ankur Kothari  <a.kothari711@gmail.com>
 *  @author  Elman Mansimov <elman.mansimov@gmail.com>
 *  @author  Michelle Pan   <panmiche@gmail.com>
 *  @version 1.0
 *  @since   2012-03-03
 */

public class Directory extends SystemItem {
	
	private Hashtable<String, SystemItem> contents; //contents of Directory
	
	/**
	 * Initialize a directory object with an empty Hashtable to hold
	 * the files and directories it contains.
	 * @param n String which is the name of Directory.
	 * @param d Directory which we are initializing.
	 */
	public Directory(String n, Directory d) {
		super(n, d);
		contents = new Hashtable<String, SystemItem>();
		addPeriods();
	}
	
	/**
	 * Helper function of constructor of Directory object.
	 * It adds "." as itself and ".." as parent (if exists) 
	 * to directory's contents. Used in command cd.
	 */
	private void addPeriods() {
		addItem(".", this);
		if (this.getParent() != null) {
			addItem("..", this.getParent());
		} else {
			addItem("..", this);
		}
		
	}
	
	/**
	 * Add item to the directory's contents.
	 * @param n String which is the name of item
	 * @param item SystemItem which can be either File or Directory
	 */
	public void addItem(String n, SystemItem item) {
		contents.put(n, item);
	}
	
	/**
	 * 
	 * Remove item from directory's contents.
	 * 
	 * @param key String which is the name of item
	 */
	public void removeItem(String key) {
		contents.get(key).setParent(null);
		contents.remove(key);
	}
	
	/**
	 * Return contents of directory.
	 * @return Hashtable which is contents of directory
	 */
	public Hashtable<String, SystemItem> getContents() {
		return contents;
	}
	
	/**
	 * Print contents of directory.
	 */
	public void printContents() {
		Enumeration<String> keys = contents.keys();
		while (keys.hasMoreElements()) {
			String itemname = keys.nextElement();
			if (itemname != "." && itemname != "..")
				System.out.println(itemname);
		}
	}
}
